//load exbase var exbase全局变量文件
//@descrip: 函数列表,直接函数名调用
@import "var";

//== 判断是否需要修复特定的平台或特定版本的浏览器
//== @param: $who:浏览器版本或平台，包括ie6-10, ff, safari, chrome, webkit, opera, khtml, svg, pie, mobile, pc;
//== @return: 浏览器版本，由此可推导出$who的值;
@function fix($who:6, $value:false) {
    @if typeof($who) == list {
        $r: null;
        @each $v in $who {
            $r: if($r == null, fix($v), $r and fix($v));
        }
        @return $r;
    }
    @if ($who == 6 or $who == ie6) and ($ex-support-for-ie6 or $ex-support-for-ie7 or $ex-support-for-ie8 or $ex-support-for-microsoft) and $default-platform != MB {
        @return ie6;
    }
    @if ($who == 7 or $who == ie7) and ($ex-support-for-ie7 or $ex-support-for-ie8 or $ex-support-for-microsoft) and $default-platform != MB {
        @return ie7;
    }
    @if ($who == 8 or $who == ie8) and ($ex-support-for-ie8 or $ex-support-for-microsoft) and $default-platform != MB {
        @return ie8;
    }
    @if ($who == 9 or $who == ie9) and ($ex-support-for-ie9 or $ex-support-for-microsoft) and $default-platform != MB {
        @return ie9;
    }
    @if ($who == 10 or $who == ie10) and ($ex-support-for-ie10 or $ex-support-for-microsoft) and $default-platform != MB {
        @return ie10;
    }
    @if ($who == ie or $who == ms) and $ex-support-for-microsoft {
        @return ie;
    }
    @if ($who == ff or $who == firefox or $who == moz) and $ex-support-for-mozilla {
        @return ff;
    }
    @if ($who == sf or $who == safari) and $ex-support-for-webkit {
        @return sf;
    }
    @if ($who == cr or $who == chrome) and $ex-support-for-webkit {
        @return cr;
    }
    @if $who == webkit and $ex-support-for-webkit {
        @return webkit;
    }
    @if $who == owg and $ex-support-for-original-webkit-gradients {
        @return owg;
    }
    @if ($who == o or $who == opera) and $ex-support-for-opera {
        @return opera;
    }
    @if $who == khtml and $ex-support-for-khtml and $default-platform != MB {
        @return khtml;
    }
    @if ($who == mobile or $who == mb or $who == MB) and $default-platform == MB {
        @return mobile;
    }
    @if ($who == PC or $who == pc or $who == SC) and $default-platform == SC {
        @return pc;
    }
    @if $who == svg and $ex-support-for-svg {
        @return svg;
    }
    @if $who == pie and $ex-support-for-pie {
        @return pie;
    }
    @return false;
}
//== 判断变量是否存在
//== @return: true or false;
@function chk($var) {
    @return if($var != '' and $var != false and $var != null and $var != unquote(''), true, false);
}
//== 统一type-of type_of，与js保持一致
@function typeof($var) {
    @return type-of($var);
}
//== 取变量第n个元素，并修复nth()函数遇到非list时报错问题
//== @param: $value:要取的变量;
//== @param: $n:列表第n个值;
//== @param: $last:为true时n超出变量的下标就取变量的最后一个值，为false时n超出下标则返回null;
@function n($value, $n:1, $last:true) {
    $v: null;
    //@if typeof($value) == list or typeof($value) == arglist {
    $len: length($value);
    @if $n > $len and $last {
        $v: nth($value, $len);
    }
    @else if $n <= $len {
        $v: nth($value, $n);
    }
    //}
    @return $v;
}
//== 把value中的颜色值转为16进制带alpha的颜色值，形如#FFc0c0c0
//== @return: color or null;
@function iehex($value: $default-color) {
    $color: null;
    @if typeof($value) == list{
        @each $v in $value {
            @if typeof($v) == color {
                $color: $v;
            }
            @else if typeof($v) == list {
                @each $w in $v {
                    @if typeof($w) == color{
                        $color: $w;
                    }
                }
            }
        }
    }
    @else if typeof($value) == color{
        $color: $value;
    }
    @if typeof($color) == color {
        @return ie-hex-str($color);
    }
    @return null;
}
//== 去掉属性值中的!important
@function no-important($value){
    $v: $value;
    @if typeof($value) == list {
        @if nth($value, length($value)) == !important {
            $v: nth($value, 1);
            @for $i from 2 through length($value) - 1 {
                $v: join($v, nth($value, $i));
            }
        }
    }
    @return $v;
}
//== 拼接属性字符串
//== @return: 以给定的$seq拼接后的结果，如$value不是list则返回原值;
@function splice($value, $seq:'-') {
    $str: null;
    @if typeof($value) == list {
        @each $v in $value {
            @if chk($v) {
                $str: if($str == null, $v, $str#{unquote($seq)}$v);
            }
        }
    }@else {
        $str: $value;
    }
    @return unquote($str);
}
//== 将给定的值或list转为逗号或空格分隔的list
@function split($value, $seq: comma) {
    $list: $null;
    @if typeof($value) == list {
        @each $v in $value {
            @if chk($v) {
                $list: if($list == null, $v, join($list, $v, $seq));
            }
        }
    }@else {
        $list: $value;
    }
    @return $list;
}
//== 从给定的值中分离出$rules和其它值
//== @param: $value:要分隔的列表;
//== @param: $which:要返回的部分。0 去除$rules中的值;1 返回$rules中的值;
//== @return: 返回$which指定的列表值;
@function split-values($value, $which:0, $rules:$default-direction) {
    $z: null;
    @if typeof($value) == list {
        @each $v in $value {
            @if $which == 0 and not index($rules, $v) or ($which == 1 and index($rules, $v)) {
                $z: if($z == null, $v, join($z, $v, space));
            }
        }
    }@else {
        @if $which == 0 and not index($rules, $value) or ($which == 1 and index($rules, $value)) {
            $z: $value;
        }
    }
    @return $z;
}
//== 根据$which拼出最终的属性
//== @param: $value:一个或两个空格分隔的值;
//== @param: $which:默认为四个方向;
//== @param: $hack:如果有hack则拼接到属性最前面;
//== @return: 一组css属性;
@function property($value, $which:$default-direction, $hack: false) {
    $property: null;
    @if $which == true {
        $which: null;
    }
    @if typeof($which) == list {
        @each $v in $which {
            $v: splice(n($value, 1) $v n($value, 2, false));
            $property: if($property == null, $v, join($property,$v));
        }
    }@else {
        $property: splice(n($value, 1) $which n($value, 2, false));
    }
    @if chk($hack) {
        @if typeof($property) == list {
            @each $v in $property {
                $v: #{$hack}#{$v};
            }
        }@else {
            $property: #{$hack}#{$property};
        }
    }
    @return $property;
}
//== 按width, style, color重排border各项并补全缺少的值
@function border-value($value1: null, $value2:null, $value3:null) {
    $width: $default-border-width;
    $style: $default-border-style;
    $color: $default-border-color;
    @if not $value1 {
        @return null;
    }
    @if typeof($value1) == list {
        $value3: n($value1, 3, false);
        $value2: nth($value1, 2);
        $value1: nth($value1, 1);
    }
    @if typeof($value1) == number {
        $width: $value1;
    }
    @else if typeof($value2) == number {
        $width: $value2;
    }
    @else if typeof($value3) == number {
        $width: $value3;
    }
    @if typeof($value1) == color {
        $color: $value1;
    }
    @else if typeof($value2) == color {
        $color: $value2;
    }
    @else if typeof($value3) == color {
        $color: $value3;
    }
    @if typeof($value1) == string {
        $style: $value1;
    }
    @else if typeof($value2) == string {
        $style: $value2;
    }
    @else if typeof($value3) == string {
        $style: $value3;
    }
    @return ($width $style $color);
}
//== css grid 跨度计算
@function span($n:1){
    @return $default-grid-width * $n + $default-grid-margin * ($n - 1);
}
